﻿using System;
using System.Configuration;
using System.Web;
using System.Collections.Generic;
using System.Web.UI.WebControls;

namespace Shuba.Web.approval_assigment
{
    public partial class persetujuan_um_approval : basePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string acl = GetUserAccess("APPROVAL_UM");

                if (!IsUserAdd(acl) && !IsUserEdit(acl))
                {
                    string msg = this.GetGlobalResourceObject("Messages", "NoPrivilege").ToString();
                    AlertAndRedirect(msg, ResolveUrl(ConfigurationManager.AppSettings["default_url"]));
                }

                BindUserCO();

                LabelTitle.Text = GetPageTitle("APPROVAL_UM");

                hidProspectID.Value = Request.QueryString["id"];

                Shuba.Business.Prospect bl = new Shuba.Business.Prospect();
                Shuba.Model.ProspectInfo bi;

                bi = bl.GetApproveUM(hidProspectID.Value);
                txtAOCode.Text = bi.AO_CODE;
                txtProspectNo.Text = bi.PROSPECT_NO;
                txtFullName.Text = bi.FULL_NAME;
                txtTglLahir.Text = bi.DATE_OF_BIRTH.ToString("yyyy") == "1900" ? "" : bi.DATE_OF_BIRTH.ToString("dd MMMM yyyy");
                txtTempatLahir.Text = bi.PLACE_OF_BIRTH;
                txtKTPName.Text = bi.KTP_NAME;
                txtJenisKelamin.Text = bi.MALE_FEMALE;
                txtKTPNo.Text = bi.KTP_NO;
                txtNPWP.Text = bi.NPWP_NO;
                txtIbuKandung.Text = bi.MOTHER_NAME;
                txtStatusNikah.Text = bi.MARITAL_STATUS;
                txtSuamiIstri.Text = bi.SPOUSE_NAME;
                tglLahirSuamiIstri.Text = bi.SPOUSE_DATE_OF_BIRTH.ToString("yyyy") == "1900" ? "" : bi.SPOUSE_DATE_OF_BIRTH.ToString("dd MMMM yyyy");
                txtHP.Text = bi.MOBILE_PHONE;
                txtOfficePhone.Text = bi.OFFICE_PHONE;
                txtHomePhone.Text = bi.RESIDENTIAL_PHONE;
                txtStatusTinggal.Text = bi.RESIDENTIAL_STATUS;
                txtLamaTinggal.Text = bi.YEAR_OF_STAY.ToString("n0");
                txtStatusTempatUsaha.Text = bi.OFFICE_STATUS;
                txtJenisUsaha.Text = bi.JENIS_USAHA;
                txtLamaUsaha.Text = bi.LAMA_USAHA;
                txtTanggungan.Text = bi.JML_TANGGUNGAN.ToString("n0");
                txtOmzet.Text = bi.OMSET_PER_HARI.ToString("n0");
                txtKodya.Text = bi.KODYA_KAB_BUSINESS;
                txtKecamatan.Text = bi.KECAMATAN_BUSINESS;
                txtKelurahan.Text = bi.KELURAHAN_BUSINESS;
                txtZIP.Text = bi.ZIP_CODE_BUSINESS;
                txtAlamat.Text = bi.ADDRESS_BUSINESS;
                txtFundingLending.Text = bi.FUNDING_LENDING;
                txtProduk.Text = bi.PRODUCT_TYPE_DESC;
                txtSalesChannel.Text = bi.SALES_CHANNEL;
                txtSiklus.Text = bi.CYCLE_STATUS;
                txtRencanaPlafon.Text = bi.RENCANA_PLAFON.ToString("n0");
                txtTypeProspect.Text = bi.PROSPECT_TYPE;
                txtDebtorType.Text = bi.DEBTOR_TYPE;
                txtChanneling.Text = bi.CHANNELING;
                txtExternal.Text = bi.EKSTERNAL;
                txtBatasBank.Text = bi.BI_GOOD_UNTIL.ToString("yyyy") == "2009" ? "" : bi.BI_GOOD_UNTIL.ToString("dd MMMM yyyy");

                //02/01/2014 - PCD Dari SPADA or MOBILE
                lblPCDFrom.Text = (bi.IS_OFFLINE_ENTRY == "1") ? "SPAMOBILE" : "SPADA";

                if (bi.UM_DECISION == "1")
                {
                    lblPCDtoCD.Text = bi.UM_DECISION_DATE.ToString("dd MMMM yyyy ") + bi.UM_DECISION_DATE.ToLongTimeString();
                }

                ddlApprove.SelectedValue = bi.UM_DECISION;
                txtKeterangan.Text = bi.UM_DECISION_NOTE;
                ddlCOAssigment.SelectedValue = bi.CO_ASSIGNED;

                BindDedup();
                ddlApprove_SelectedIndexChanged(null, EventArgs.Empty);
            }
        }

        void BindDedup()
        {
            Shuba.Business.Prospect bl = new Shuba.Business.Prospect();
            Shuba.Model.ProspectInfo bi;

            bi = bl.GetApproveUM(hidProspectID.Value);
            DateTime datededup = bl.GetDateDedupResult(hidProspectID.Value);
            Int16 DedupKe = bl.GetRequestDedupKe(hidProspectID.Value);
            
            //Update By SL 30 Oct 2013, untuk antisipasi data null
            if(bi.PROGRESS_STATUS == null) bi.PROGRESS_STATUS = "";
            if (bi.IS_DEDUP == null) bi.IS_DEDUP = "";

            if (bi.PROGRESS_STATUS.Trim() == "PCD" || bi.UPDATE_DATE < DateTime.Now.AddDays(7))
            {
                lblDedupKe.Text = DedupKe.ToString();
                lblDedupDate.Text = datededup.ToString("yyyy") == "2009" ? "" : datededup.ToString("dd MMMM yyyy ") + datededup.ToLongTimeString();
            }
            else
            {
                lblDedupKe.Text = bi.NO_OF_CALLS.ToString();
                lblDedupDate.Text = bi.FAP_DATE.ToString("yyyy") == "2009" ? "" : bi.FAP_DATE.ToString("dd MMMM yyyy ") + bi.FAP_DATE.ToLongTimeString();
            }

            if (!bl.IsHasRequest(hidProspectID.Value))
            {
                lblFinalDeup.Text = "Sedang dilakukan Request Dedup";
                btnReqDedup.Enabled = false;
            }

            List<Shuba.Model.DedupResultInfo> dri;
            if (bi.PROGRESS_STATUS.Trim() == "PCD" || bi.UPDATE_DATE < DateTime.Now.AddDays(7))
            {
                dri = bl.GetDedupResult(hidProspectID.Value, DedupKe);
            }
            else dri = bl.GetDedupResult(hidProspectID.Value, bi.NO_OF_CALLS);

            gvList.DataSource = dri;
            gvList.DataBind();

            if (gvList.Rows.Count > 0 || bi.IS_DEDUP.Trim() == "1" || bl.IsHasRequest(hidProspectID.Value))
            {
                lblFinalDeup.Text = "Proses Final Dedup telah selesai";
            }
        }

        void BindUserCO()
        {
            Shuba.Business.User bl = new Shuba.Business.User();
            List<Shuba.Model.UserInfo> list = bl.GetUserCO(Session["UnitID"].ToString());
            ddlCOAssigment.DataSource = list;
            ddlCOAssigment.DataTextField = "Name";
            ddlCOAssigment.DataValueField = "Nip";
            ddlCOAssigment.DataBind();

            if (ddlCOAssigment.Items.Count > 0)
            {
                ListItem ListAll = new ListItem();
                ListAll.Value = "PILIH";
                ListAll.Text = "- Pilih dengan benar -";
                ddlCOAssigment.Items.Insert(0, ListAll);
            }
        }

        protected void btnApprove_Click(object sender, EventArgs e)
        {
            try
            {
                string msgA = "";
                if (ddlApprove.SelectedValue == "0" && txtKeterangan.Text == "")
                {
                    msgA = "Anda memilih \"Menolak\" prospect ini, Mohon isi Keterangan Penolakan";
                    Alert(msgA);
                }

                /*
                if (ddlApprove.SelectedValue == "1" && ddlCOAssigment.SelectedValue == "PILIH")
                {
                    msgA = "Anda memilih \"Approve\", Mohon pilih CO Assignment";
                    Alert(msgA);
                }
                */

                Shuba.Business.AppParam syspar = new Shuba.Business.AppParam();
                Shuba.Business.Prospect bl = new Shuba.Business.Prospect();

                if (ddlApprove.SelectedValue == "1" && syspar.Get().IS_NEED_FINAL_DEDUP == "1" && !bl.IsHasRequest(hidProspectID.Value))
                {
                    msgA = "Anda belum mendapatkan hasil Final Dedup";
                    Alert(msgA);
                }

                if (msgA == "")
                {
                    Shuba.Model.ProspectInfo en = new Shuba.Model.ProspectInfo();
                    en.PROSPECT_ID = hidProspectID.Value;
                    en.UM_DECISION = ddlApprove.SelectedValue;
                    en.UM_DECISION_NOTE = txtKeterangan.Text;
                    en.UM_DECISION_DATE = ProcessingDate();
                    en.UPDATE_BY = HttpContext.Current.User.Identity.Name;
                    en.UPDATE_DATE = ProcessingDate();
                    en.PROSPECT_FINISH_TIME = ProcessingDate();
                    en.CO_ASSIGNED = ddlApprove.SelectedValue == "PILIH" ? "" : ddlCOAssigment.SelectedValue;
                    if (bl.UpdateKeputusanUM(en) > 0)
                    {
                        AddLog("UPDATE", " UPDATE PROSPECT KEPUTUSAN UM");
                        AlertAndRedirect("Sukses menyimpan data", "persetujuan_um_list.aspx");
                    }
                }
            }
            catch (Exception) { }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("persetujuan_um_list.aspx");
        }

        protected void ddlApprove_SelectedIndexChanged(object sender, EventArgs e)
        {
            Shuba.Business.Prospect bl = new Shuba.Business.Prospect();
            Shuba.Model.ProspectInfo bi;

            bi = bl.GetApproveUM(hidProspectID.Value);

            //Update By SL 30 Oct 2013, untuk antisipasi data null
            if (bi.PROGRESS_STATUS == null) bi.PROGRESS_STATUS = "";

            if (bi.PROGRESS_STATUS.Trim() == "CD" || bi.PROGRESS_STATUS.Trim() == "RJU" || bi.PROGRESS_STATUS.Trim() == "RJS")
            {
                btnApprove.Visible = false;
                btnCancel.Visible = false;
                btnReqDedup.Visible = false;
                ddlCOAssigment.Enabled = false;
                ddlApprove.Enabled = false;
                txtKeterangan.Enabled = false;
            }

            if (ddlApprove.SelectedValue == "1" && bi.PROGRESS_STATUS.Trim() == "PCD")
            {
                ddlCOAssigment.Enabled = true;
                txtKeterangan.Text = "";
                txtKeterangan.Enabled = false;
            }
            if (ddlApprove.SelectedValue == "0" && bi.PROGRESS_STATUS.Trim() == "PCD")
            {
                ddlCOAssigment.Enabled = false;
                txtKeterangan.Text = "";
                txtKeterangan.Enabled = true;
            }
        }

        protected void gvList_Sorting(object sender, System.Web.UI.WebControls.GridViewSortEventArgs e)
        {

        }

        protected void btnReqDedup_Click(object sender, EventArgs e)
        {
            Shuba.Business.Prospect bl = new Shuba.Business.Prospect();
            if (bl.RequestDedup(hidProspectID.Value) == 1) Alert("Masih ada final dedup yang sedang diproses");
            if (bl.RequestDedup(hidProspectID.Value) == 0) Alert("Request dedup berhasil");
            BindDedup();
        }
    }
}